﻿#include "eqptinfo.h"

eqptinfo::eqptinfo()
{
    xmlread();
}
// 运行时长读
QString eqptinfo::operhread()
{
    if(xmldata.length()>0){
        return xmldata[0];
    }else {
        return "";
    }
}

// 运行时长写
bool eqptinfo::operhwrite(QString operh)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "operh") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(operh);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}

QString eqptinfo::operThreRead(){
    if(xmldata.length()>1){
        return  xmldata[1];
    }else {
        return "";
    }
}

// 检测时长读
QString eqptinfo::durahread()
{
    if(xmldata.length()>2){
        return xmldata[2];
    }else {
        return "";
    }
}
// 检测时长写
bool eqptinfo::durahwrite(QString durah)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "durah") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(durah);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}

QString eqptinfo::duraThreRead(){
    if(xmldata.length()>3){
        return  xmldata[3];
    }else {
        return "";
    }
}

// 实验次数读
QString eqptinfo::timesread()
{
    if(xmldata.length()>4){
        return xmldata[4];
    }else {
        return "";
    }
}

// 实验次数写
bool eqptinfo::timeswrite(QString times)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "times") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(times);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}

QString eqptinfo::timesThreRead(){
    if(xmldata.length()>5){
        return  xmldata[5];
    }else {
        return "";
    }
}

// 总检测样本数读
QString eqptinfo::sampelsumread()
{
    if(xmldata.length()>6){
        return xmldata[6];
    }else {
        return "";
    }
}
// 总检测样本数写
bool eqptinfo::sampelsumwrite(QString sampelsum)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "sampelsum") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(sampelsum);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}

QString eqptinfo::sampleSumThreRead(){
    if(xmldata.length()>7){
        return  xmldata[7];
    }else {
        return "";
    }
}

// xml数据读取
void eqptinfo::xmlread()
{
    QDomDocument doc;
    QString fileName ="EqptInfo.xml";
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << fileName;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << fileName;
        file.close();
    }
    file.close();
    QDomElement root = doc.documentElement();
    QDomNode n = root.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement(); // 尝试转换为元素
        if (!e.isNull()) {
            QString tagName = e.tagName();
            QString textContent = e.text();
            xmldata.append(textContent);
        }
        n = n.nextSibling();
    }
}

// 软件使用时限
bool eqptinfo::swlifewrite(QString swlife)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "swlife") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(swlife);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}
// 软件使用次数
bool eqptinfo::swtimeswrite(QString swtimes)
{
    QDomDocument doc;
    QString filePath = "EqptInfo.xml";
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file:" << filePath;
        return false;
    }

    if (!doc.setContent(&file)) {
        qWarning() << "Failed to parse XML file:" << filePath;
        file.close();
        return false;
    }

    file.close();

    // 获取 <info> 元素，它应该包含其他子元素
    QDomElement infoElement = doc.documentElement();

    // 遍历 <info> 元素的子元素，找到匹配的 elementName
    QDomNode n = infoElement.firstChild();
    while (!n.isNull()) {
        QDomElement e = n.toElement();
        if (!e.isNull() && e.tagName() == "swtimes") {
            // 设置新值
            QDomNode oldNode = e.firstChild();
            e.firstChild().setNodeValue(swtimes);
            QDomNode newNode = e.firstChild();
            e.replaceChild(newNode,oldNode);
            break; // 找到后退出循环
        }
        n = n.nextSibling();
    }

    // 保存修改后的 XML 文件
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning() << "Failed to open file for writing:" << filePath;
    }

    QTextStream out(&file);
    doc.save(out, 4); // 第二个参数是缩进级别
    file.close();
    return true;
}

// 运行时长读
QString eqptinfo::calculateDurationread()
{
    if(xmldata.length()>11){
        return  xmldata[11];
    }else {
        return "";
    }
}

QString eqptinfo::passwordRead(){
    if(xmldata.length()>12)
    {
        return  xmldata[12];
    }
    else
    {
        return "";
    }
}

QString eqptinfo::serverIpRead(){
    if(xmldata.length()>13){
        return  xmldata[13];
    }
    else {
        return "";
    }
}

QString eqptinfo::serverPortRead(){
    if(xmldata.length()>14){
        return  xmldata[14];
    }
    else {
        return "";
    }
}
